import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  title: '',
  department: ''
  // roles: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_DEPARTMENT: (state, department) => {
    state.department = department
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_TITLE: (state, title) => {
    state.title = title
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { identity_type, identifier, credential } = userInfo
    return new Promise((resolve, reject) => {
      login({
        userauth: {
          identity_type: identity_type,
          identifier: identifier.trim(),
          credential: credential
        }
      }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getInfo(state.token).then(response => {
  //       const { data } = response
  //       if (!data) {
  //         reject('验证失败，请重新登录.')
  //       }
  //       console.log(data)
  //       const { user, user_auths, user_info } = data
  //
  //       // roles must be a non-empty array
  //       if (!user_auths.identifier || user_auths.identifier.length <= 0) {
  //         reject('getInfo: roles must be a non-null array!')
  //       }
  //
  //       commit('SET_AUTHS', [user_auths.identifier])
  //       commit('SET_NAME', [user.nickname])
  //       commit('SET_INFO', [user_info.title])
  //       commit('SET_INTRODUCTION', [user_auths.identifier])
  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // }

  // user logout
  // logout({ commit, state, dispatch }) {
  //   return new Promise((resolve, reject) => {
  //     logout(state.token).then(() => {
  //       commit('SET_TOKEN')
  //       //commit('SET_ROLES', [])
  //       removeToken()
  //       // resetRouter()
  //       this.$router.push('/login')
  //
  //       // reset visited views and cached views
  //       // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
  //       dispatch('tagsView/delAllViews', null, { root: true })
  //
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // remove token
  // resetToken({ commit }) {
  //   return new Promise(resolve => {
  //     commit('SET_TOKEN', '')
  //     //commit('SET_ROLES', [])
  //     removeToken()
  //     resolve()
  //   })
  // },
//
//   // dynamically modify permissions
//   async changeRoles({ commit, dispatch }, role) {
//     // const token = role + '-token'
//
//     commit('SET_TOKEN', token)
//     setToken(token)

    //const { roles } = await dispatch('getInfo')

    // resetRouter()
    //
    // // generate accessible routes map based on roles
    // const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
    // // dynamically add accessible routes
    // router.addRoutes(accessRoutes)

    // reset visited views and cached views
  //   dispatch('tagsView/delAllViews', null, { root: true })
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
